<?php

error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

//微信API初始验证
if($_GET['echostr']){
	check_weixin_api();
	die();
}


//路由跳转
if ($_GET['a']=="getinfo"){
	
	getinfo();die();
	
}elseif($_GET['a']=="getcode"){

	getcode();die();
	
}

else{
	
	getinfo();die();
	
}




function getinfo(){
	
	 $appid = 'wx0d52e598e211c841';
                
     $appsecret   = 'c698f5d6e006d6b5bd4e024cb32b6a20';
     
     $redirect = urlencode('http://ceyanzhi.app.yanxishe.cc/index.php?a=getcode');
     
     $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
     
     echo '<meta http-equiv="Refresh" content="0; url='.$url.'" /> ';	
	
}

function getcode(){
	
/* 		if(!$_GET['code']){
			
			echo '<meta http-equiv="Refresh" content="0; url=http://3d.yanxishe.cc/index.php">';
				
		} */
	
	    $appid = 'wx0d52e598e211c841';
                
        $appsecret   = 'c698f5d6e006d6b5bd4e024cb32b6a20';
        
        $code = $_GET['code'];
        
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        $data = array();
        $res = http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
        
        echo '<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />';
        
        $resArr = json_decode($res);
        
		$access_token = $resArr->access_token;
        $openid = $resArr->openid;
        
        
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        
        $res2 =  http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));
        
        $resArr2 = json_decode($res2);
        
        $nickname  = $resArr2->nickname;
        $pic       = $resArr2->headimgurl;
		$sex       = $resArr2->sex;
		$openid    = $resArr2->openid;
		
		
?>

<div style="display:none;">
<form id='form1' name='form1' method='post' action='h52.php?sex=<?=$sex?>'>
              <input name='ok' type='hidden' value='ok' />
</form>
</div>

<script>

document.form1.submit();

</script>


<?php
		
		
		//跳转到H5正式页面
		//header("Location:h52.php?sex={$sex}");
		//exit;
		
		/* if($sex ==1){
		//检测到男性用户
			
		$id = mt_rand(1,8);
		
		header("Location:show.php?sex=1&id={$id}"); 

        exit;
			
		}else{
		//检测到女性和其它用户	
		
		$id = mt_rand(1,5);
		
		header("Location:show.php?sex=2&id={$id}"); 

        exit;
		} */
		
		/* echo "性别：{$sex}"; */
        
       /*  echo "昵称：{$nickname}<br> ";
		
        echo "头像：<img src='$pic' width='20%'>";
		
        echo "<hr>";
        
        print_r($res2);
		
		echo "<hr>";
		
		var_dump($resArr2);

		echo "<hr>";  */
		
	
}   /* function getcode */   


function check_weixin_api(){

define(TOKEN,'weixin1234');

$timestamp = $_GET['timestamp'];
$nonce     = $_GET['nonce'];
$token     = TOKEN;

$signature = $_GET['signature'];

$tmpArr = array($timestamp,$nonce,$token);

sort($tmpArr,SORT_STRING);

$tmpStr = sha1(implode($tmpArr));

if ($tmpStr == $signature  && $_GET['echostr']){

    echo $_GET['echostr'];

}else{
    //echo '没有Echo Str ，FALSE';
    echo "FALSE";
}

}


/**

 * 发送HTTP请求方法

 * @param  string $url    请求URL

 * @param  array  $params 请求参数

 * @param  string $method 请求方法GET/POST

 * @return array  $data   响应数据

 */
function http($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件

            $params = $multi ? $params : http_build_query($params);

            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}
?>
 


 